package code.lewdy.lottery;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import code.lewdy.db.Database;
import code.lewdy.db.JdbcTemple;
import code.lewdy.db.JdbcUtil;
import code.lewdy.time.TimeHelper;

/**彩票开奖时间
 * @author Administrator
 *
 */
@Entity
@Table
public class LotteryOpenTime {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		JdbcUtil jdbc = new JdbcUtil(Database.TYPE_SQLSERVER);
		List<LotteryOpenTime> os = (List<LotteryOpenTime>) jdbc.list(LotteryOpenTime.class.getName(), "where lotteryId=9", 1, 120);
		for(LotteryOpenTime o: os){
			System.out.println(o.getNumberIdSuffix()+":"+o.getBeginTimeFormat()+"|"+o.getOpenTimeFormat());
		}
		if(true){
			return;
		}
		*/
		//更新
		//System.out.println(JdbcTemple.executeUpdate("delete from LotteryOpenTime where numberIdSuffix>'096' and lotteryId=8"));
		
		int lotteryId = 8;
		String lotteryName = "重庆时时彩";
		int beginTime = TimeHelper.getSecondsByTime("23:56:00");  //上期结束时间
		int firstOpenTime = TimeHelper.getSecondsByTime("00:01:00"); 
		int maxTime =   TimeHelper.getSecondsByTime("00:01:00");
		int total = 100; //total
		int suffix = 120;  //begin 期号
		int delay = 5;  //每期间隔分钟
		
		String suffixStr = "";
		int results = 0;
		int openTime = beginTime;
		for(int i=0; i<total; i++){
			if((suffix+"").length() == 1){
				suffixStr = "00"+suffix;
			}else if((suffix+"").length() == 2){
				suffixStr = "0"+suffix;
			}else{
				suffixStr =  ""+suffix;
			}
			if(i == 0){
				openTime = firstOpenTime;
			}else{
				openTime = beginTime + 60 * delay; //每期间隔分钟
			}

			if(openTime > maxTime ){
				break;
			}
			LotteryOpenTime ot = new LotteryOpenTime();
			ot.setBeginTime(beginTime);
			ot.setLotteryId(lotteryId);
			ot.setLotteryName(lotteryName);
			ot.setNumberIdSuffix(suffixStr);
			
			ot.setOpenTime(openTime);
			results = insert(ot);
			System.out.println(suffixStr+":"+results);
			beginTime = openTime;
			if(lotteryId == 9){
				beginTime = beginTime + 10; //每期延后10秒
			}
			suffix ++;
		}
		
	}
	
	
	/**插入
	 * @param lot
	 * @return
	 */
	public static int insert(LotteryOpenTime lot){
		int results = 0;
		String sql = "insert into LotteryOpenTime (lotteryId, lotteryName," +
				"numberIdSuffix, openTime, beginTime) values ("+lot.getLotteryId()+", '" +
						lot.getLotteryName()+"','"+lot.getNumberIdSuffix()+"',"+lot.getOpenTime()+"," +
								+lot.getBeginTime()+")";
		results = JdbcTemple.executeUpdate(sql);
		return results;
	}
	
	@Transient
	public String getOpenTimeFormat(){
		return TimeHelper.turnSecondToHHMMSS(openTime);
	}
	
	@Transient
	public String getBeginTimeFormat(){
		return TimeHelper.turnSecondToHHMMSS(beginTime);
	}
	
	private int id;
	private int lotteryId;
	private String lotteryName;
	private String numberIdSuffix; //期号后缀，如期号20120615108中的108部分
	private int openTime; //开奖时间，以秒数记，以便转换和比较，如01:01:30记为3600+60+30=3690
	private int beginTime; //时间段开始时间，以秒数记，以便转换和比较，如01:01:30记为3600+60+30=3690
	
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Column(length=20)
	public String getNumberIdSuffix() {
		return numberIdSuffix;
	}
	public void setNumberIdSuffix(String numberIdSuffix) {
		this.numberIdSuffix = numberIdSuffix;
	}
	public int getOpenTime() {
		return openTime;
	}
	public void setOpenTime(int openTime) {
		this.openTime = openTime;
	}
	public int getBeginTime() {
		return beginTime;
	}
	public void setBeginTime(int beginTime) {
		this.beginTime = beginTime;
	}
	public void setLotteryId(int lotteryId) {
		this.lotteryId = lotteryId;
	}
	public int getLotteryId() {
		return lotteryId;
	}
	public void setLotteryName(String lotteryName) {
		this.lotteryName = lotteryName;
	}
	@Column(length=30)
	public String getLotteryName() {
		return lotteryName;
	}
	

}
